package com.hexiang.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hexiang.domain.CommonResponse;
import com.hexiang.domain.User;
import com.hexiang.http.CommonResponseHttpClient;
import com.hexiang.service.UserHttpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.config.RequestConfig;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class UserHttpServiceImpl implements UserHttpService {

    private static final String USER_HTTP_URL = "https://localhost:8443";
    @Resource
    private ObjectMapper objectMapper;
    @Resource
    private CommonResponseHttpClient commonResponseHttpClient;

    @Override
    public CommonResponse<Serializable> getUsers() {
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(2000)
                .setConnectionRequestTimeout(2000)
                .setSocketTimeout(5000).build();
        return commonResponseHttpClient.doGet(USER_HTTP_URL + "/users", requestConfig);
    }

    @Override
    public CommonResponse getUsersTimeOut() {
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(2000)
                .setConnectionRequestTimeout(2000)
                .setSocketTimeout(1000).build();
        return commonResponseHttpClient.doGet(USER_HTTP_URL + "/users", requestConfig);
    }

    @Override
    public CommonResponse getUsersErr() {
        return commonResponseHttpClient.doGet(USER_HTTP_URL + "/user/err");
    }

    @Override
    public CommonResponse<Serializable> getUserByName(String name) {
        return commonResponseHttpClient.doGet(USER_HTTP_URL + "/user/" + name);
    }

    @Override
    public CommonResponse<Serializable> addUser(String name, int age) {
        Map<String, String> params = new HashMap<>(2);
        params.put("name", name);
        params.put("age", String.valueOf(age));
        return commonResponseHttpClient.doPost(USER_HTTP_URL + "/user", params);
    }

    @Override
    public CommonResponse<Serializable> deleteUserById(Long id) {
        return commonResponseHttpClient.doDelete(USER_HTTP_URL + "/user/" + id);
    }

    @Override
    public CommonResponse<Serializable> updateUser(User user) {
        CommonResponse<Serializable> commonResponse;
        String userJson;
        try {
            userJson = objectMapper.writeValueAsString(user);
        } catch (JsonProcessingException e) {
            log.error("JsonProcessingException", e);
            return CommonResponse.builder().code(400).msg("参数错误").succ(false).build();
        }
        if (StringUtils.isBlank(userJson)) {
            commonResponse = CommonResponse.builder().code(400).msg("参数错误").succ(false).build();
        } else {
            commonResponse = commonResponseHttpClient.doPut(USER_HTTP_URL + "/user/", userJson);
        }

        return commonResponse;
    }

    @Override
    public CommonResponse<Serializable> getUserPage(Integer pageNum, Integer pageSize) {
        Map<String, String> params = new HashMap<>(2);
        params.put("pageNum", String.valueOf(pageNum));
        params.put("pageSize", String.valueOf(pageSize));
        return commonResponseHttpClient.doGet(USER_HTTP_URL + "/user/page", params);
    }
}